package ru.spb.osll.myfit.common.filter;


import ru.spb.osll.myfit.common.PageExecutingResult;


/**
 * User: Alexander Opeykin
 * Date: 3/27/12
 */
public class MatchFilter implements PageResultFilter {
  private String m_key;
  protected String m_value = "";

  public MatchFilter(String key) {
    this.m_key = key;
  }

  public MatchFilter(String key, String value) {
    m_key = key;
    m_value = value;
  }

  @Override
  public boolean filter(PageExecutingResult result) {
    if (!result.getMetadata().containsKey(m_key)) {
      return false;
    }
    String value = result.getMetadata().get(m_key);
    return m_value.equals(value);
  }

  @Override
  public String serialize() {
    if (m_value == null) {
      return m_key.trim() + "=";
    } else {
      return m_key.trim() + "=" + m_value.trim();
    }
  }

  public String getKey() {
    return m_key;
  }

  public void setValue(String value) {
    m_value = value;
  }

  public String getValue() {
    return m_value;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    MatchFilter filter = (MatchFilter) o;

    if (m_key != null ? !m_key.equals(filter.m_key) : filter.m_key != null) return false;
    if (m_value != null ? !m_value.equals(filter.m_value) : filter.m_value != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = m_key != null ? m_key.hashCode() : 0;
    result = 31 * result + (m_value != null ? m_value.hashCode() : 0);
    return result;
  }
}
